package transport

import (
	"context"
	"encoding/json"
	"fmt"
	"gowork/studentGoKit/client/endpoint"
	"net/http"
)

func StudentInsertRequest(ctx context.Context, req *http.Request, r interface{}) error{
	insertRequest := r.(endpoint.StudentRequest)
	req.URL.Path += "/insert?"+"id="+insertRequest.Id+"&name="+insertRequest.Name+"&birth="+insertRequest.Birth+"&sex="+insertRequest.Sex
	fmt.Println("服务端请求地址:", req.URL.Path)
	return nil
}

func StudentDeleteRequest(ctx context.Context, req *http.Request, r interface{}) error{
	deleteRequest := r.(endpoint.StudentNameRequest)
	req.URL.Path += "/delete?"+"name="+deleteRequest.Name
	return nil
}

func StudentUpdateRequest(ctx context.Context, req *http.Request, r interface{}) error{
	updateRequest := r.(endpoint.StudentRequest)
	req.URL.Path += "/update?"+"id="+updateRequest.Id+"&name="+updateRequest.Name+"&birth="+updateRequest.Birth+"&sex="+updateRequest.Sex
	return nil
}

func StudentQueryRequest(ctx context.Context, req *http.Request, r interface{}) error{
	req.URL.Path += "/query"
	return nil

}

func StudentMapResponse(ctx context.Context, res *http.Response)(response interface{}, err error){
	var mapResponse endpoint.StudentMapResponse
	fmt.Println(res.Body)
	err = json.NewDecoder(res.Body).Decode(&mapResponse)
	if err != nil{
		fmt.Println(err)
		return nil, err
	}
	fmt.Println("返回值", mapResponse)
	return mapResponse, nil
}

func StudentQueryResponse(ctx context.Context, res *http.Response)(response interface{}, err error){
	var queryResponse endpoint.StudentResponse
	err = json.NewDecoder(res.Body).Decode(&queryResponse)
	if err != nil{
		return nil, err
	}

	return queryResponse, nil
}